package com.jmb.api.controller.APITEST.test;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmb.api.common.result.Result;
import com.jmb.api.model.entity.ApiTestCase;
import com.jmb.api.service.ApiTestCaseService;
import com.jmb.api.vo.api.ApiTestCaseVo;
import com.jmb.api.vo.api.test.ApiTestCaseListTO;
import com.jmb.api.vo.api.test.ApiTestCaseListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "测试用例")
@RequestMapping("/api/test")
@RestController
public class ApiTestController {
    @Autowired
    private ApiTestCaseService apiTestService;


    @Operation(summary = "保存测试用例")
    @PostMapping("save")
    public Result save(@RequestBody @Valid ApiTestCaseVo apiTestCase){
        apiTestService.saveApiTest(apiTestCase);
        return Result.ok();
    }
    @Operation(summary = "编辑测试用例")
    @PostMapping("edit")
    public Result edit(@RequestBody @Valid ApiTestCaseVo apiTestCase){
        apiTestService.editApiTest(apiTestCase);
        return Result.ok();
    }
    @Operation(summary = "查询测试用例")
    @GetMapping("{caseId}")
    public Result<ApiTestCaseVo> getApiTestCaseByCaseId(@PathVariable Integer caseId){
        ApiTestCaseVo apiTestCaseVo = apiTestService.getApiTestCaseByCaseId(caseId);
        return Result.ok(apiTestCaseVo);
    }
    @Operation(summary = "删除测试用例")
    @DeleteMapping("{caseId}")
    public Result deleteApiTestCaseByCaseId(@PathVariable Integer caseId){
        // 删除需要删除其他的关联数据
        apiTestService.removeByCaseId(caseId);
        return Result.ok();
    }
    @Operation(summary = "测试用例列表")
    @PostMapping("list")
    public Result<IPage<ApiTestCaseListVO>> list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, ApiTestCaseListTO apiTestCaseVo){
        IPage<ApiTestCaseListVO> page = new Page<>(pageNum,pageSize);
        IPage<ApiTestCaseListVO> list = apiTestService.getApiTestCaseList(page,apiTestCaseVo);
        return Result.ok(list);
    }
    @Operation(summary = "用例树形展示")
    @GetMapping("tree")
    public Result caseTree(Integer level,Integer id){

        return Result.ok(apiTestService.caseTree(level,id));
    }
    @Operation(summary = "执行接口测试用例")
    @GetMapping("execute/{caseId}")
    public Result execute(@PathVariable Integer caseId, HttpServletRequest request){

        return Result.ok();
    }

}
